<template>
  <view> </view>
</template>

<script setup lang="ts">
import { onLoad } from "@dcloudio/uni-app";

onLoad((options) => {
  const requestTask = wx.request({
    method: "POST",
    timeout: 30000,
    enableChunked: true,
    responseType: "text",
    url: "https://open-test.kukewang.com/assistant/chat",
    data: { message: "你好" },
    header: {
      "Content-Type": "application/json",
      "kk-token":
        "eyJ0eXAiOiJKV1QiLCJraWQiOjEwMDAzLCJhbGciOiJIUzI1NiJ9.eyJsZWV3YXlfdGltZSI6MTAsInVpZCI6IjQ2OTQ2NTk4MzI4NDg1NDc4NCIsInNlcnZlcl9uYW1lIjoia3VrZWFkbWluIiwiZXhwaXJhdGlvbl90aW1lIjozNjAwMCwiZnJvbSI6IndlYiIsImV4cCI6MTcyNDE4MzU5MywicmVmcmVzaF9leHAiOjE3MjQxOTc5OTMsInJlZnJlc2hfdGltZSI6NTA0MDB9.c5V72MQQ3WYtzvDDHFLXuKilXAKfFyOjtxCMZWpG8Ek",
      "kk-request-id": 12111312,
      "tenant-id": "",
      "org-id": "",
    },
  });
  requestTask.onHeadersReceived((res: any) => {
    console.log("都是付款了； ", res);
  });
  requestTask.onChunkReceived((res: any) => handleChunk(res));
});

const handleChunk = (res: any) => {
  const arrayBuffer = res.data;
  const uint8Array = new Uint8Array(arrayBuffer);
  const str = new TextDecoder("utf-8").decode(uint8Array);
  const lines = str.split("data:");
  lines.forEach((line) => {
    if (!line.trim()) {
      return;
    }
    if (!line.includes(`:welcome`)) {
      const json = JSON.parse(line);
    } else {
    }
  });
};
</script>

<style lang="scss" scoped></style>
